package com.fadadianqi.logistics.inventory.dao.ibatis;

import com.fadadianqi.commons.GenericDAOSuport;
import com.fadadianqi.logistics.inventory.dao.ItemDAO;
import com.fadadianqi.logistics.inventory.model.Item;
import com.yeqiangwei.commons.util.DateTimeUtils;
import com.yeqiangwei.commons.util.Pagination;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("inventoryItemDAO")
public class ItemDAOImpl extends GenericDAOSuport<String, Item>
        implements ItemDAO {

    @Override
    public Item getNormalByGoodsId(String goodsId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_GOODS_ID, goodsId);
        map.put(Item.PROP_STATUS, Item.STATUS_NORMAL);
        return super.getByPropertys(map);
    }

    @Override
    public Item getNormalItemByGoodsIdAndStorehouseId(String goodsId,
                                                      String storehouseId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_GOODS_ID, goodsId);
        map.put(Item.PROP_STATUS, Item.STATUS_NORMAL);
        map.put(Item.PROP_STOREHOUSE_ID, storehouseId);
        Object d = getSqlMapClientTemplate().queryForObject(getNamespace() + ".getNormalItemByGoodsIdAndStorehouseId", map);
        if (d != null) {
            return (Item) d;
        }
        return null;
    }

    @Override
    public Item getNormalItemByBatchId(String batchId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_BATCH_ID, batchId);
        map.put(Item.PROP_STATUS, Item.STATUS_NORMAL);
        return super.getByPropertys(map);
    }

    @Override
    public List<Item> findByBatchId(String batchId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_BATCH_ID, batchId);
        return super.findByPropertys(map);
    }

    @Override
    public int countNormalStatusByGoodsId(String goodsId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_GOODS_ID, goodsId);
        map.put(Item.PROP_STATUS, Item.STATUS_NORMAL);
        long stime = DateTimeUtils.now();
        Integer i = null;
        i = (Integer) getSqlMapClientTemplate().queryForObject(getNamespace() + ".countNormalStatusByGoodsId", map);
        getLog().info("countNormalStatusByGoodsId(Map map): proceed in " + (DateTimeUtils.now() - stime));
        return i;
    }

    @Override
    public int countNormalStatusByBatchId(String batchId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_BATCH_ID, batchId);
        map.put(Item.PROP_STATUS, Item.STATUS_NORMAL);
        long stime = DateTimeUtils.now();
        Integer i = null;
        i = (Integer) getSqlMapClientTemplate().queryForObject(getNamespace() + ".countNormalStatusByBatchId", map);
        getLog().info("countNormalStatusByBatchId(Map map): proceed in " + (DateTimeUtils.now() - stime));
        return i;
    }

    @Override
    public int countByBrandIdAndCategoryId(String brandId, String categoryId,
                                           int status) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_BRAND_ID, brandId);
        map.put(Item.PROP_CATEGORY_ID, categoryId);
        map.put(Item.PROP_STATUS, Item.STATUS_NORMAL);
        return super.countAll(map);
    }

    /**
     * 统计库存
     */
    @Override
    public Double sumCost() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_STATUS, Item.STATUS_NORMAL);
        Object d = getSqlMapClientTemplate().queryForObject(getNamespace() + ".sumCost", map);
        if (d != null) {
            return (Double) d;
        } else {
            return 0D;
        }
    }

    @Override
    public Double sumPurchase() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_STATUS, Item.STATUS_NORMAL);
        Object d = getSqlMapClientTemplate().queryForObject(getNamespace() + ".sumPurchase", map);
        if (d != null) {
            return (Double) d;
        } else {
            return 0D;
        }
    }

    @Override
    public Double sumPurchaseByBrandIdAndCategoryId(String brandId,
                                                    String categoryId, int status) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_BRAND_ID, brandId);
        map.put(Item.PROP_CATEGORY_ID, categoryId);
        map.put(Item.PROP_STATUS, Item.STATUS_NORMAL);
        Object d = getSqlMapClientTemplate().queryForObject(getNamespace() + ".sumPurchase", map);
        if (d != null) {
            return (Double) d;
        } else {
            return 0D;
        }
    }

    @Override
    public Double sumCostByBrandIdAndCategoryId(String brandId,
                                                String categoryId, int status) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_BRAND_ID, brandId);
        map.put(Item.PROP_CATEGORY_ID, categoryId);
        map.put(Item.PROP_STATUS, Item.STATUS_NORMAL);
        Object d = getSqlMapClientTemplate().queryForObject(getNamespace() + ".sumCost", map);
        if (d != null) {
            return (Double) d;
        } else {
            return 0D;
        }
    }

    @Override
    public List<Item> findLeCost(Double maxCost) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Class<?> getObjectClass() {
        return Item.class;
    }

    @Override
    protected String getTableName() {
        return "inventory";
    }

    /**
     * 统计货物在库房的数量
     */
    @Override
    public int countByGoodsIdAndStorehouseIdAndStatus(String goodsId
            , String storehouseId, int status) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_GOODS_ID, goodsId);
        map.put(Item.PROP_STOREHOUSE_ID, storehouseId);
        map.put(Item.PROP_STATUS, Item.STATUS_NORMAL);
        return (Integer) getSqlMapClientTemplate().queryForObject(getNamespace() + ".countInStorehouse", map);
    }

    @Override
    public void dispatchStorehouse(String goodsId, String fromId,
                                   String storehouseId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_GOODS_ID, goodsId);
        map.put(Item.PROP_STOREHOUSE_ID, storehouseId);
        map.put("fromId", fromId);
        getSqlMapClientTemplate().update(getNamespace() + ".dispatchStorehouse", map);
    }

    @Override
    public List<Item> findByGoodsIdAndStorehouseIdAndStatus(String goodsId, String storehouseId,
                                                            int status, Pagination pagination) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_GOODS_ID, goodsId);
        map.put(Item.PROP_STOREHOUSE_ID, storehouseId);
        map.put(Item.PROP_STATUS, status);
        map.put("pagination", pagination);
        return super.findByPropertys(map);
    }

}
